//
//  BFHomeViewController.m
//  BFoodSystem
//
//  Created by 陈名正 on 2017/3/13.
//  Copyright © 2017年 陈名正. All rights reserved.
//

#import "BFHomeViewController.h"
#import "BFLoginServices.h"
#import "BFShopMangerController.h"
#import "BFLoginServices.h"
#import "BFQueryServices.h"
#import "BFQueryDailyModel.h"
#import "BFMenuView.h"
#import "BFHomeTopView.h"
#import "BFManagerViewController.h"
@interface BFHomeViewController ()<UINavigationControllerDelegate,BFHomeTopViewDelegate,UIScrollViewDelegate>


@property (nonatomic, strong)BFHomeTopView *topView;

@property (nonatomic,strong)BFQueryDailyModel *dailyModel;
/** 右侧tab*/
/** 选中索引*/
@property (nonatomic,assign) NSInteger selectIndex;

/** 左View*/
@property (nonatomic,strong) UIView *leftView;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UIScrollView *contentView;
@property (nonatomic, strong) UIView *rightView;
/** n*/
@property (nonatomic,strong) NSMutableArray *titles;
@property (nonatomic,strong) NSMutableArray *selectTitles;

@property (nonatomic,strong) NSMutableArray *btnArr;

@end

@implementation BFHomeViewController
- (NSMutableArray *)titles{
    if (!_titles) {
        _titles = [NSMutableArray arrayWithObjects:@"dataNormal", @"shopNormal",@"waiterNormal",@"vipNormal",nil];
    }
    return _titles;
}

- (NSMutableArray *)selectTitles{
    if (!_selectTitles) {
        _selectTitles = [NSMutableArray arrayWithObjects:@"dataClick", @"iconShopClick",@"waiterClick",@"vipClick",nil];
    }
    return _selectTitles;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self getShopSaleInfo];
//    self.view.backgroundColor = [UIColor redColor];
    UIImage *backGroundImage=[UIImage imageNamed:@"imgStart"];
    self.view.contentMode=UIViewContentModeScaleAspectFill;
    self.view.layer.contents=(__bridge id _Nullable)(backGroundImage.CGImage);
    
    
    [self setupSubViews];
    
    self.navigationController.delegate = self;
    if (IOS11) {
         [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }
    self.btnArr=[NSMutableArray array];
    [self addChildViewControllers];

    [self setupLeftView];
    [self setupRightView];
    
    
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    BOOL  isShow = [viewController isKindOfClass:[self class]];

    [self.navigationController setNavigationBarHidden:isShow animated:YES];
}

- (void)setupSubViews{

    
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"BFHomeTopView" owner:nil options:nil] lastObject];
    self.topView.delegate = self;
    
    CGFloat top = 30;
    if(iPhoneX){
        top = 60;
    }
    self.topView.frame = CGRectMake(5, top, SCREEN_WIDTH-10, 200);
    [self.view addSubview:self.topView];
    
    BFMenuView *menuView = [[BFMenuView alloc] initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT, SCREEN_WIDTH, 30)];
    [self.view addSubview:menuView];
   
}

- (void)setupLeftView{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(10, self.topView.y+self.topView.height, 60, SCREEN_HEIGHT-(self.topView.y+self.topView.height)-TabBarHeight-10)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    self.leftView = topView;
    topView.layer.cornerRadius = 10;
    topView.layer.masksToBounds = YES;
    

    
    for (int i = 0 ; i<self.titles.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:self.titles[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:self.selectTitles[i]] forState:UIControlStateSelected];
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(5,i*50+10,50, 50);
        btn.tag = i;
        [topView addSubview:btn];
        
        if (i == 0) {
            btn.selected = YES;
            self.selectBtn = btn;
        }
        
        [self.btnArr addObject:btn];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, i*50+60,30 , 2)];
        lineView.backgroundColor = [UIColor colorWithHex:BF_COLOR_L16];
        [topView addSubview:lineView];
        
    }
    
    
}
- (void)addChildViewControllers
{
    BFManagerViewController *firstVC = [BFManagerViewController new];
    firstVC.index = 0;
    [self addChildViewController:firstVC];
    
    
    BFManagerViewController *firstVC1 = [BFManagerViewController new];
    firstVC1.index = 1;
    [self addChildViewController:firstVC1];
    
    BFManagerViewController *firstVC2 = [BFManagerViewController new];
    firstVC2.index = 2;
    [self addChildViewController:firstVC2];
    
    BFManagerViewController *firstVC3 = [BFManagerViewController new];
    firstVC3.index = 3;
    [self addChildViewController:firstVC3];
    
}
- (void)setupRightView{

    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(80,self.leftView.y, self.view.width-90, self.leftView.height)];
    rightView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:rightView];
    self.rightView = rightView;
    
    UIScrollView *contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, rightView.width, rightView.height)];
    contentView.pagingEnabled = YES;
    contentView.contentSize = CGSizeMake(0,self.rightView.height * self.titles.count);
    contentView.bounces = NO;
    contentView.delegate = self;
    contentView.showsVerticalScrollIndicator = NO;
    contentView.showsHorizontalScrollIndicator = NO;
    [self.rightView insertSubview:contentView atIndex:0];
    self.contentView = contentView;
    [self scrollViewDidEndScrollingAnimation:contentView];
}

- (void)btnClick:(UIButton *)btn
{
  
    if (btn != _selectBtn) {
        btn.selected = YES;
        _selectBtn.selected = !_selectBtn.selected;
        _selectBtn = btn;
    }
    CGPoint point = self.contentView.contentOffset;
    point.y = btn.tag * self.contentView.height;
    [self.contentView setContentOffset:point animated:YES];
    
}
#pragma mark ------scrollView的代理方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.y / self.rightView.height;
    UIViewController *vc = self.childViewControllers[index];
    vc.view.y = scrollView.contentOffset.y;
    vc.view.height = scrollView.height;
    vc.view.x = 0;
    [scrollView addSubview:vc.view];
    //
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    int index = scrollView.contentOffset.y / self.rightView.height;

    WS(weakSelf);
    [weakSelf btnClick:self.btnArr[index]];

}


//获取当前时间
- (NSString *)getTodayStr{
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    [formater setDateFormat:@"YYYY-MM-dd"];
    NSDate *nowDate = [NSDate date];
    NSString *nowStr = [formater stringFromDate:nowDate];
    return nowStr;
}

//数据刷新
- (void)refreshHeadViewData{

//    [BFUtils showProgressHUDWithTitle:@"获取店铺流水数据中..." inView:self.view animated:YES];
    [[BFQueryServices alloc] updateStatusInfoWithSuccessBlock:^(id result) {
        [BFUtils hideProgressHUDInView:self.view delegate:nil animated:YES afterDelay:0];
        [self getShopSaleInfo];

    } errorCode:^(NSInteger errorCode, NSString *errorMessage) {
        [BFUtils hideProgressHUDInView:self.view delegate:nil animated:YES afterDelay:0];
        [BFUtils showAlertController:0 title:@"" message:errorMessage];
    } Failure:^(NSError *error) {
        [BFUtils hideProgressHUDInView:self.view delegate:nil animated:YES afterDelay:0];
//        [BFUtils showAlertController:0 title:@"" message:@"网络错误"];
    }];

}


- (void)getShopSaleInfo{
    [[BFQueryServices alloc] queryStatisticsDailyWithStartTime:[self getTodayStr] endTime:[self getTodayStr] SuccessBlock:^(id result) {
        NSArray *dailyModelArr = (NSArray *)result;
        self.dailyModel = dailyModelArr.firstObject;
        [self.topView configTopInfoWith:self.dailyModel];
        
    } errorCode:^(NSInteger errorCode, NSString *errorMessage) {
        [BFUtils showAlertController:0 title:@"" message:errorMessage];
    } Failure:^(NSError *error) {
        [BFUtils showAlertController:0 title:@"" message:@"网络错误"];
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
